//
//  RegisterVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/2/6.
//
// 【注册】注册页面

import Foundation
import UIKit

class RegisterVC: SubLevelViewController {
    let registV = RegisterView()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setupUI()
    }

    // MARK: - Actions

    @objc func submitTapped() {
        let text = registV.phoneTF.titleTF.text ?? ""
        if Utils.isPhoneNumber(phone: text) == false {
            self.view.showToast(witMsg: "请输入正确的手机号!")
            return
        }
        
        if registV.pswTF.titleTF.text != registV.againPswTF.titleTF.text {
            self.view.showToast(witMsg: "两次密码不一致，请重新输入。")
            return
        }
        
        API.register(withParam: registV.registerM) { responseModel in
            DispatchQueue.main.async {
                if responseModel.errorCode == .Success {
                    Utils.app.setupUI()
                    Utils.app.window?.showToast(witMsg: "注册成功")
                } else {
                    self.registV.imgCodeTapped()
                    self.view.showToast(witMsg: responseModel.errorMessage)
                }
            }
        }
    }

    // MARK: - Setup

    func setupUI() {
        registV.confrimBtn.addTarget(self, action: #selector(submitTapped), for: .touchUpInside)
        view.addSubview(registV)
        registV.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }
    }
}
